//
// Created by francklinson on 2021/10/23 AT 21:52.
//
#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <algorithm>
#include <functional>
#include <climits>
#include <string>
#include "../myTool.h"

//using namespace std;
class Str {
public:
    Str() = default;

    Str(const char *String) {
        printf("Created!\n");
        m_size = strlen(String);
        m_Data = new char[m_size];
        memcpy(m_Data, String, m_size);
    }

    Str(const Str &other) {
        printf("Copied!\n");
        m_size = other.m_size;
        m_Data = new char[other.m_size];
        memcpy(m_Data, other.m_Data, m_size);
    }

    // 移动构造 窃取右值的内容 并将右值的指针重置
    Str(Str &&other) noexcept {
        printf("Move!\n");
        m_size = other.m_size;
        m_Data = other.m_Data;
        // 浅拷贝
        other.m_size = 0;
        other.m_Data = nullptr;
    }

    ~Str() {
        printf("Deleted!\n");
        delete m_Data;
    }

    void print() {
        for (int i = 0; i < m_size; ++i) {
            printf("%c", m_Data[i]);
        }
        printf("\n");
    }


private:
    char *m_Data;
    uint32_t m_size;
};

class Entity {
public:
    Entity(const Str &name) :
            m_Name(name) {

    }

    Entity(Str &&name) :
            m_Name(std::move(name)) {

    }

    void printName() {
        m_Name.print();
    }

private:
    Str m_Name;
};

int main() {
    Entity entity(Str("Francklinson")); // const char*-->Str(rvalue)-->entity(&&)-->Str(Str &&)
    entity.printName();
    return 0;
}

